package com.suning.sawp.web.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.Principal;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.suning.sawp.constants.LoggerConstants;
import com.suning.sawp.constants.PromotionErrorConstants;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.precharge.CommonRtn;
import com.suning.sawp.dto.promotion.CheckRegisterRtn;
import com.suning.sawp.dto.promotion.InnerCouponDetail;
import com.suning.sawp.dto.promotion.InnerCouponRecordRtn;
import com.suning.sawp.dto.promotion.InnerRemain;
import com.suning.sawp.intf.promotion.InnerCouponService;
import com.suning.sawp.web.exception.MyGsonView;
/**
 * 
 * 内部优惠券<br> 
 * 〈功能详细描述〉
 *
 * @author Wuxiao
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@RequestMapping("/innerCoupon")
public class InnerCouponController extends DjBaseController{

    private static final Logger LOGGER = LoggerFactory.getLogger(LoggerConstants.SAWP_SERVICE);
    
    @Resource
    InnerCouponService innerCouponService;
    
    /**
     * 
     * 功能描述: 获取券列表<br>
     * 〈功能详细描述〉
     *
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryCouponList.do")
    public MyGsonView queryCouponList(Principal principal){
        ReturnMsg<List<InnerCouponDetail>>  ret = new ReturnMsg<List<InnerCouponDetail>>();
        String staffId = principal.getName();
        try{
            ret = innerCouponService.queryCouponList(staffId);
        }catch(Exception e){
            ret.setError(PromotionErrorConstants.INNER_COUPON_ERR_E999,
                    PromotionErrorConstants.INNER_COUPON_ERR_MAP);
            LOGGER.error(String.format("InnerCouponController.queryCouponList 查询内部券活动列表失败,工号:%s,", staffId), e);
        }
        return setGsonView(ret);
    }
    /**
     * 
     * 功能描述:跳转失效跳转 <br>
     * 〈功能详细描述〉
     *
     * @param topicId
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping(value = "/login.do", produces = "application/json; charset=utf-8")
    public void login(String redirectUrl, HttpServletRequest request, HttpServletResponse response) {
        StringBuilder url = new StringBuilder(redirectUrl);
        try {
            response.sendRedirect(url.toString());
        } catch (UnsupportedEncodingException e1) {
            LOGGER.error("", e1);
        } catch (IOException e) {
            LOGGER.error("", e);
        }
    }
    /**
     * 
     * 功能描述:查询剩余次数 <br>
     * 〈功能详细描述〉
     *
     * @param principal
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryRemainTimes.do")
    public MyGsonView queryRemainTimes(Principal principal){
        ReturnMsg<InnerRemain>  ret = new ReturnMsg<InnerRemain>();
        String staffId = principal.getName();
        try{
            ret = innerCouponService.queryRemainTimes(staffId);
        }catch(Exception e){
            ret.setError(PromotionErrorConstants.INNER_COUPON_ERR_E999,
                    PromotionErrorConstants.INNER_COUPON_ERR_MAP);
            LOGGER.error(String.format("InnerCouponController.queryRemainTimes 查询内部券当月剩余发送次数失败,工号:%s,", staffId), e);
        }
        return setGsonView(ret);
    }
    
    /**
     * 
     * 功能描述: 查询手机号是否注册会员<br>
     * 〈功能详细描述〉
     *
     * @param principal
     * @param mobile
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/checkMemerByMobile.do")
    public MyGsonView checkMemberByMobile(Principal principal,String mobile){
        ReturnMsg<CheckRegisterRtn>  ret = new ReturnMsg<CheckRegisterRtn>();
        String staffId = principal.getName();
        try{
             ret = innerCouponService.checkRegister(mobile, staffId);
        }catch(Exception e){
            ret.setError(PromotionErrorConstants.INNER_COUPON_ERR_E999,
                    PromotionErrorConstants.INNER_COUPON_ERR_MAP);
            LOGGER.error(String.format("InnerCouponController.checkMemberByMobile 校验手机号是否是会员失败,工号:%s,手机号:%s", staffId,mobile), e);
        }
        return setGsonView(ret);
    }
    
    /**
     * 
     * 功能描述: 查询发券记录<br>
     * 〈功能详细描述〉
     *
     * @param principal
     * @param pageSize
     * @param pageNo
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/couponRecord.do")
    public MyGsonView queryRecord(Principal principal,Integer pageSize,Integer pageNo){
        ReturnMsg<InnerCouponRecordRtn>  ret = new ReturnMsg<InnerCouponRecordRtn>();
        String staffId = principal.getName();
        try{
            ret = innerCouponService.queryCouponRecord(staffId, pageSize, pageNo);
        }catch(Exception e){
            ret.setError(PromotionErrorConstants.INNER_COUPON_ERR_E999,
                    PromotionErrorConstants.INNER_COUPON_ERR_MAP);
            LOGGER.error(String.format("InnerCouponController.queryRecord 查询内部发券记录失败,工号:%s,pageSize:%s,pageNo:%s", staffId,pageSize,pageNo), e);
        }
        return setGsonView(ret);
    }
    /**
     * 
     * 功能描述:发券 <br>
     * 〈功能详细描述〉
     *
     * @param principal
     * @param activityId
     * @param securityKey
     * @param mobile
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/sendCoupon.do")
    public MyGsonView sendCoupon(Principal principal,String activityId,String activityName,String securityKey,String mobile){
        ReturnMsg<CommonRtn>  ret = new ReturnMsg<CommonRtn>();
        String staffId = principal.getName();
        LOGGER.debug(String.format("InnerCouponController.sendCoupon request ,staffId:%s,activityId:%s,mobile:%s,securityKey:%s", staffId,activityId,mobile,securityKey));
        try{
            ret = innerCouponService.sendCoupon(mobile, activityId, activityName,securityKey, staffId);
        }catch(Exception e){
            ret.setError(PromotionErrorConstants.INNER_COUPON_ERR_E999,
                    PromotionErrorConstants.INNER_COUPON_ERR_MAP);
            LOGGER.error(String.format("InnerCouponController.sendCoupon 发券失败,staffId:%s,activityId:%s,mobile:%s,securityKey:%s", staffId,activityId,mobile,securityKey), e);
        }
        return setGsonView(ret);
    }
}
